package com.zhuojy.equity.mall.admin.web.controller.salary;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuojy.equity.mall.admin.common.constant.JwtConstants;
import com.zhuojy.equity.mall.admin.common.entity.bo.salary.PaySlipsBO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.PaySlipsPO;
import com.zhuojy.equity.mall.admin.common.entity.po.salary.TSalaryPO;
import com.zhuojy.equity.mall.admin.common.entity.vo.salary.PaySlipsVO;
import com.zhuojy.equity.mall.admin.common.pojo.ResultEntity;
import com.zhuojy.equity.mall.admin.manager.token.TokenManager;
import com.zhuojy.equity.mall.admin.service.salary.PaySlipsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: 工资条模块
 * @author chen
 * @date 2024/4/8 19:43
 * @version 1.0
 */
@RestController
@RequestMapping("paySlips")
@RequiredArgsConstructor
@Api(tags = "工资条模块")
public class PaySlipsController {
    private final PaySlipsService paySlipsService;

    private final TokenManager tokenManager;
    /**
     * @description: 添加工资条
     * @param:
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 14:04:53
     */
    @PostMapping("addPaySlips")
    @ApiOperation(value = "添加工资条",httpMethod = "POST",notes = "添加工资条")
    public ResultEntity addPayrollItems(@RequestBody PaySlipsBO paySlipsBO, HttpServletRequest request){
        try {

            paySlipsService.addPayrollItems(paySlipsBO,request);
            return ResultEntity.OK("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("新增失败");
        }
    }

    /**
     * @description: 修改工资条
     * @param:
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 14:04:53
     */
    @PostMapping("updatePaySlips")
    @ApiOperation(value = "修改工资条",httpMethod = "POST",notes = "修改工资条")
    public ResultEntity updatePaySlips(@RequestBody PaySlipsBO paySlipsBO, HttpServletRequest request){
        try {
            paySlipsService.updatePaySlips(paySlipsBO,request);
            return ResultEntity.OK("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("修改失败");
        }
    }

    /**
     * @description: 获取薪资方案下拉列表
     * @param:
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 14:24:53
     */
    @GetMapping("getSalaryList")
    @ApiOperation(value = "获取薪资方案下拉列表",httpMethod = "GET",notes = "获取薪资方案下拉列表")
    public ResultEntity getSalaryList(){
        try {
            List<TSalaryPO> list = paySlipsService.getSalaryList();
            return ResultEntity.OK(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("获取薪资下拉列表失败!!!");
        }
    }
    /**
     * @description: 获取薪资方案工资条列表
     * @param:
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 14:24:53
     */
    @PostMapping("getPaySlipsList")
    @ApiOperation(value = "获取薪资方案工资条列表",httpMethod = "POST",notes = "获取薪资方案工资条列表")
    public ResultEntity getPaySlipsList(@RequestBody PaySlipsBO paySlipsBO){
        try {
            Map map = paySlipsService.getPaySlipsList(paySlipsBO);
            return ResultEntity.OK(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("获取工资条列表失败!!!");
        }
    }
    /**
     * @description: 修改工资条状态 ccccccccdcecee
     * @param id
     * @param state
     * @param request
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 20:04:40
     */
    @GetMapping("switchPaySlip")
    @ApiOperation(value = "修改工资条状态",httpMethod = "GET",notes = "修改工资条状态")
    public ResultEntity switchPaySlip(@RequestParam("id") Long id,@RequestParam("state") Integer state,HttpServletRequest request){
        try {
            String userNameFromToken = tokenManager.getUserNameFromToken(request.getHeader(JwtConstants.TOKEN_HEADER).substring(6));
            paySlipsService.update(Wrappers.lambdaUpdate(PaySlipsPO.class).eq(PaySlipsPO::getId,id).set(PaySlipsPO::getState,state).set(PaySlipsPO::getUpdateTime,new Date()).set(PaySlipsPO::getCreateBy,userNameFromToken));
            return ResultEntity.OK("状态修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("状态修改失败!!!");
        }
    }
    /**
     * @description: 删除薪资方案
     * @param id
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 20:04:40
     */
    @GetMapping("deletePaySlip")
    @ApiOperation(value = "删除薪资方案",httpMethod = "GET",notes = "删除薪资方案")
    public ResultEntity deletePaySlip(@RequestParam("id") Long id){
        try {
            paySlipsService.remove(Wrappers.lambdaQuery(PaySlipsPO.class).eq(PaySlipsPO::getId,id));
            return ResultEntity.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("删除失败!!!");
        }
    }
    /**
     * @description: 回显薪资方案
     * @param id
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 20:04:40
     */
    @GetMapping("findPaySlip")
    @ApiOperation(value = "回显薪资方案",httpMethod = "GET",notes = "回显薪资方案")
    public ResultEntity findPaySlip(@RequestParam("id") Long id){
        try {
            PaySlipsVO paySlip = paySlipsService.findPaySlip(id);
            return ResultEntity.OK(paySlip);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("回显失败!!!");
        }
    }
    /**
     * @description: 删除薪资方案
     * @param id
     * @return com.zhuojy.equity.mall.admin.common.pojo.ResultEntity
     * @author chen
     * @date: 2024-04-09 20:04:40
     */
    @GetMapping("deletePayroll")
    @ApiOperation(value = "删除薪资方案",httpMethod = "GET",notes = "删除薪资方案")
    public ResultEntity deletePayroll(@RequestParam("id") Long id){
        try {
            paySlipsService.remove(Wrappers.lambdaQuery(PaySlipsPO.class).eq(PaySlipsPO::getId,id));
            return ResultEntity.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.ERROR("删除失败!!!");
        }
    }


}
